<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>员工管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="{{asset('/layuiadmin/layui/css/layui.css')}}" media="all">
    <link rel="stylesheet" href="{{asset('/layuiadmin/style/admin.css')}}" media="all">
    <style>
        .edit{background-color: #ed9c3a;}
        .shenhe{background-color: #429488;}
        .see{background-color: #7cb717;}
        .cur{color:#009688;}
        .del {background-color: #e85052;}    /*.laytable-cell-1-school_icon{height:100%;}*/
        input::-webkit-outer-spin-button,
        input::-webkit-inner-spin-button {
            -webkit-appearance: none;
        }
        input[type="number"]{
            -moz-appearance: textfield;
        }
        .view_qr{background-color: #4fd9e0;}
        #code{width: 190px;margin-right:10px;height: 190px;margin-right:10px;margin: 20px auto;}
        #codeimg{width: 100%;}
    </style>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-fluid">
                <div class="layui-row layui-col-space15">
                    <div class="layui-col-md12" style="margin-top:0px">
                        <div class="layui-card">
                            <div class="layui-card-header">员工管理</div>
                            <div class="layui-card-body">
                                <div class="layui-btn-container" style="font-size:14px;">
                                    <a class="layui-btn layui-btn-primary buildcode" style="background-color:#3475c3;border-radius: 5px;border:none;color:#fff;">添加员工</a>
                                    </div>

                                <table class="layui-hide" id="test-table-page" lay-filter="test-table-page"></table>
                                <script type="text/html" id="statusTpl">
                                    @{{#  if(d.status == 1){ }}
                                    <span>开启</span>
                                    @{{#  } else { }}
                                    <span class="cur">关闭</span>
                                    @{{#  } }}
                                    </script>

                                <script type="text/html" id="table-content-list">
                                    <a class="layui-btn layui-btn-normal layui-btn-xs viewqr" lay-event="viewqr" style="border-radius:3.5px;background-color: #FFB800;">显示二维码</a>
                                    <a class="layui-btn layui-btn-normal layui-btn-xs down" lay-event="down" style="border-radius:3.5px;background-color: #FFB800;">下载二维码</a>
                                    <a class="layui-btn layui-btn-normal layui-btn-xs storelist" lay-event="storelist" style="border-radius:3.5px;background-color: #FFB800;">门店列表</a>
                                    <a class="layui-btn layui-btn-normal layui-btn-xs update" lay-event="update" style="border-radius:3.5px;background-color: #FFB800;">编辑</a>
                                    </script>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div id="add_rate" class="hide" style="display: none;background-color: #fff;">
    <div class="layui-card-body" style="padding: 15px;">
        <div class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label">手机号:</label>
                <div class="layui-input-block">
                    <input type="number" placeholder="请输入手机号" class="layui-input phone">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">名称:</label>
                <div class="layui-input-block">
                    <input type="text" placeholder="请输入名称" class="layui-input name">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">状态:</label>
                <div class="layui-input-block">
                    <div class="layui-col-md12">
                        <input type="checkbox" name="zzz" lay-skin="switch" id="addstatus" lay-text="开启|关闭" checked>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <div class="layui-footer" style="left: 0;">
                        <button class="layui-btn submit" style="border-radius:5px;margin-top:20px;margin-left:30%">确定</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div id="edit_rate" class="hide" style="display: none;background-color: #fff;">
    <div class="layui-card-body" style="padding: 15px;">
        <div class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label">手机号:</label>
                <div class="layui-input-block">
                    <input type="number" placeholder="请输入手机号" class="layui-input phone">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">名称:</label>
                <div class="layui-input-block">
                    <input type="text" placeholder="请输入名称" class="layui-input name">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">状态:</label>
                <div class="layui-input-block">
                    <div class="layui-col-md12">
                        <input type="checkbox" name="editstatus" lay-skin="switch" id="editstatus" lay-text="开启|关闭">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <div class="layui-footer" style="left: 0;">
                        <button class="layui-btn submitEdit" style="border-radius:5px;margin-top:20px;margin-left:30%">确定</button>
                    </div>
                </div>
            </div>
            <input type="hidden" id="id">
        </div>
    </div>
</div>
<div id="view_qr" class="hide" style="display: none;background-color: #fff;">
    <div class="layui-card-body" style="padding: 15px;">
        <div class="layui-form">
            <div class="layui-form-item">
                <div style="display:flex;color: black;font-size: 16px;text-align: center;">
                    <div>员工姓名：</div>
                    <div class="workername"></div>
                </div>
                <div id="code"><img id="codeimg" src="" /> </div>
            </div>
        </div>
    </div>
</div>
<!-- 显示门店列表 -->
<div id="openStorelist" style="display: none; background-color: #fff;">
	<div style="margin: 10px 0;display:flex;color: black;font-size: 16px;text-align: center;">
		<span>员工姓名：</span>
		<div class="workername"></div>
	</div>
	<table id="openlistTable" lay-filter="openlistTable"></table>
</div>

<script src="{{asset('/layuiadmin/layui/layui.js')}}"></script>
<script src="{{asset('/layuiadmin/layui/jquery-2.1.4.js')}}"></script>
<script src="{{asset('/layuiadmin/layui/jquery.qrcode.min.js')}}"></script>
<script>
    var token = localStorage.getItem("Usertoken");
    var agentName = localStorage.getItem("agentName");
    var str=location.search;
    var user_id=str.split('?')[1];

    layui.config({
        base: '../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index','form', 'table','laydate'], function(){
        var $ = layui.$
            ,admin = layui.admin
            ,table = layui.table
            ,form = layui.form
            ,laydate = layui.laydate;

        // 未登录,跳转登录页面
        $(document).ready(function(){
            if(token==null){
                window.location.href="{{url('/user/login')}}";
            }
        });

        table.render({
            elem: '#test-table-page'
            ,url: "{{url('/api/v2/incoming/Worker/lists')}}"
            ,method: 'get'
            ,where:{
                token:token,
                user_id:user_id
            }
            ,request:{
                pageName: 'page',
                limitName: 'pagesize'
            }
            ,page: true
            ,cellMinWidth: 150
            ,cols: [[
                {field: 'id', title: 'id'}
                ,{field: 'user_id',  title: '代理商Id'}
                ,{field: 'name',  title: '名称'}
                ,{field: 'phone',  title: '手机号'}
                ,{field: 'status',  title: '状态',templet: '#statusTpl'}
                ,{field: 'created_at',  title: '生成时间'}
                ,{width: 350, align:'center', fixed: 'right', toolbar: '#table-content-list',title: '操作'}
            ]]
            ,response: {
                statusName: 'status' //数据状态的字段名称，默认：code
                ,statusCode: 1 //成功的状态码，默认：0
                ,msgName: 'message' //状态信息的字段名称，默认：msg
                ,countName: 'total' //数据总数的字段名称，默认：count
                ,dataName: 'data' //数据列表的字段名称，默认：data
            }
            ,done: function(res, curr, count){
//          console.log(res);
                $('th').css({'font-weight': 'bold', 'font-size': '15','color': 'black','background':'linear-gradient(#f2f2f2,#cfcfcf)'});	//进行表头样式设置
            }
        });

        table.on('tool(test-table-page)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var e = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象
            //          console.log(e);

            if(layEvent === 'down'){
                // window.location.href=e.path;
                //图片的地址 
                fetch(e.path)
                // 获取 blob 对象
                .then(res=>res.blob())
                .then(blob=>{
                // 创建a标签
                var link = document.createElement('a');
                // 设置a标签为不可见
                link.style.display = 'none';
                // 将a标签添加到body
                document.body.appendChild(link);
                // 生成Blob URL并设置给a标签的href属性
                var url = window.URL.createObjectURL(blob);
                link.href = url;
                // 设置a标签的download
                link.download = e.phone + '.png';
                // 模拟点击事件进行下载
                link.click();
                //下载完成后清理URL对象和a标签
                window.URL.revokeObjectURL(url);
                document.body.removeChild(link); 
                })
            }else if(layEvent === 'viewqr'){
                $('#view_qr .workername').html(e.name);
                $('#codeimg').attr('src',e.path)
                // $('#code').qrcode(e.path);
                layer.open({
                    type: 1,
                    title: '二维码',
                    closeBtn: 0,
                    area: '516px',
                    skin: 'layui-layer-nobg', //没有背景色
                    shadeClose: true,
                    content: $('#view_qr')
                });
            }else if(layEvent === 'update'){
                $('#edit_rate .phone').val(e.phone);
                $('#edit_rate .name').val(e.name);
                $('#edit_rate #id').val(e.id);
                console.log(e.status,'status------------')
                if (e.status == 1) {
                    $("input[name=editstatus]").prop("checked", true)
                    layui.form.render();
                    // $('#edit_rate #editstatus').prop('checked', true);
                } else {
                    console.log(e.status,'jinlai')
                    $("input[name=editstatus]").prop("checked", false)
                    layui.form.render();
                    // $('#edit_rate #editstatus').prop('checked', false);
                }
                layer.open({
                    type: 1,
                    title: '编辑',
                    closeBtn: 0,
                    area: '516px',
                    skin: 'layui-layer-nobg', //没有背景色
                    shadeClose: true,
                    content: $('#edit_rate')
                });
            }else if(layEvent === 'storelist'){
                $('#openStorelist .workername').html(e.name);
                table.render({
                    elem: '#openlistTable'
                    ,height:480
                    ,url: "{{url('/api/user/store_pc_lists')}}"
                    ,method: 'get'
                    ,where:{
                        token:token,
                        worker_phone:e.phone
                    }
                    ,request:{
                        pageName: 'page',
                        limitName: 'pagesize'
                    }
                    ,page: true
                    ,cols: [[
                        {field: 'store_id', title: '门店id',width:120}
                        ,{field: 'store_name',  title: '门店名称',}
                    ]]
                    ,response: {
                        statusName: 'status' //数据状态的字段名称，默认：code
                        ,statusCode: 1 //成功的状态码，默认：0
                        ,msgName: 'message' //状态信息的字段名称，默认：msg
                        ,countName: 'total' //数据总数的字段名称，默认：count
                        ,dataName: 'data' //数据列表的字段名称，默认：data
                    }
                    ,done: function(res, curr, count){
        //          console.log(res);
                        $('th').css({'font-weight': 'bold', 'font-size': '15','color': 'black','background':'linear-gradient(#f2f2f2,#cfcfcf)'});	//进行表头样式设置
                    }
                });
                layer.open({
                    type: 1,
                    title: '门店列表',
                    closeBtn: 0,
                    area: '516px',
                    skin: 'layui-layer-nobg', //没有背景色
                    shadeClose: true,
                    content: $('#openStorelist')
                });
            }
            });

        $('.buildcode').click(function(){
            $('#add_rate .phone').val('');
            $('#add_rate .name').val('');
            $('#add_rate #addstatus').prop('checked', true);
            layer.open({
                type: 1,
                title: '添加',
                closeBtn: 0,
                area: '516px',
                skin: 'layui-layer-nobg', //没有背景色
                shadeClose: true,
                content: $('#add_rate')
            });
        });

        $('.submit').click(function(){
            var index = layer.load(2);
            $.post("{{url('/api/v2/incoming/Worker/add')}}",
                {
                    token:token
                    ,phone:$('#add_rate .phone').val()
                    ,name:$('#add_rate .name').val()
                    ,status:$('#add_rate #addstatus').is(':checked') ? '1' : '0'
                    ,user_id:user_id
                },
                function(res){
                    //console.log(res);
                    layer.close(index);

                    if(res.status==1){
                        layer.msg('提交成功', {
                            offset: '50px'
                            ,icon: 1
                            ,time: 2000
                        },function(){
                            layer.closeAll();
                           //执行重载
                            table.reload('test-table-page', {
                                page: {
                                    curr: 1
                                },
                                where: {
                                }
                            });
                        });
                    }else{
                        layer.msg(res.message, {
                            offset: '50px'
                            ,icon: 2
                            ,time: 3000
                        });
                    }
                },"json");
        })
        // 编辑
        $('.submitEdit').click(function(){
            console.log(22222)
            var index = layer.load(2);
            $.ajax({
                url : "{{url('/api/v2/incoming/Worker/update')}}",
                data : {
                    token:token
                    ,id:$('#edit_rate #id').val()
                    ,phone:$('#edit_rate .phone').val()
                    ,name:$('#edit_rate .name').val()
                    ,status:$('#edit_rate #editstatus').is(':checked') ? '1' : '0'
                    ,user_id:user_id
                },
                type : 'put',
                dataType:'json',
                success : function(res) {
                    layer.close(index);
                    console.log(res,'res----')
                    if(res.status==1){
                        layer.msg('提交成功', {
                            offset: '50px'
                            ,icon: 1
                            ,time: 2000
                        },function(){
                            layer.closeAll();
                           //执行重载
                            table.reload('test-table-page', {
                                page: {
                                    curr: 1
                                },
                                where: {
                                }
                            });
                        });
                    }else{
                        layer.msg(res.message, {
                            offset: '50px'
                            ,icon: 2
                            ,time: 3000
                        });
                    }
                },
                error : function(data) {
                    alert('查找板块报错');
                }
            });
        })
    });

</script>

</body>
</html>
